package com.han.transfer.process;

import com.han.transfer.constant.BaseParameter;
import com.han.transfer.resource.ResourceManager;
import com.han.transfer.util.Painter;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @author HanJiafeng
 * @date 2021/6/8 10:45
 * @desc todo
 */
public abstract class AbstractGenerator extends ResourceManager implements Generator {
    /**
     * 转账金额字符开始X坐标
     */
    private int amountX;

    @Override
    public BufferedImage drawWechatBillDetail(String phoneTime, int battery, BaseParameter.Payment payment, String payee, String amount, String paymentMode, String transferTime) {

        loadResource();

        Graphics2D billGraphics2D = super.getTemplateImage().createGraphics();
        billGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        drawHeader(billGraphics2D, phoneTime, battery);

        drawPayee(billGraphics2D, payment, payee);

        drawAmount(billGraphics2D, amount);

        drawPaymentInfo(billGraphics2D, paymentMode, transferTime);

        drawTail(billGraphics2D);

        return super.getTemplateImage();
    }

    /**
     * 画头部内容（账单详情，全部账单，右上角手机时间）
     *
     * @param billGraphics2D billGraphics2D
     * @param phoneTime      手机时间
     * @param battery        电池电量
     */
    protected abstract void drawHeader(Graphics2D billGraphics2D, String phoneTime, int battery);

    /**
     * 画收款人
     *
     * @param billGraphics2D billGraphics2D
     * @param payment        支付方式
     * @param payee          收款人
     */
    protected abstract void drawPayee(Graphics2D billGraphics2D, BaseParameter.Payment payment, String payee);

    /**
     * 根据指定字符，将转账金额图片画到预定图片中
     *
     * @param billGraphics2D billGraphics2D
     * @param character      character
     */
    private void drawAmountCharacter(Graphics2D billGraphics2D, char character) {
        switch (character) {
            case '-' -> {
                billGraphics2D.drawImage(getAmountImageBar(), null, amountX, 30);
                amountX += 44;
            }
            case '.' -> {
                amountX += 6;
                billGraphics2D.drawImage(getAmountImageDot(), null, amountX, 58);
                amountX += 16;
            }
            case '0' -> {
                amountX += 6;
                billGraphics2D.drawImage(getAmountImage0(), null, amountX, 0);
                amountX += 46;
            }
            case '1' -> {
                amountX += 10;
                billGraphics2D.drawImage(getAmountImage1(), null, amountX, 3);
                amountX += 37;
            }
            case '2' -> {
                amountX += 6;
                billGraphics2D.drawImage(getAmountImage2(), null, amountX, 2);
                amountX += 47;
            }
            case '3' -> {
                amountX += 5;
                billGraphics2D.drawImage(getAmountImage3(), null, amountX, 1);
                amountX += 47;
            }
            case '4' -> {
                amountX += 3;
                billGraphics2D.drawImage(getAmountImage4(), null, amountX, 2);
                amountX += 49;
            }
            case '5' -> {
                amountX += 5;
                billGraphics2D.drawImage(getAmountImage5(), null, amountX, 1);
                amountX += 48;
            }
            case '6' -> {
                amountX += 5;
                billGraphics2D.drawImage(getAmountImage6(), null, amountX, 1);
                amountX += 48;
            }
            case '7' -> {
                amountX += 4;
                billGraphics2D.drawImage(getAmountImage7(), null, amountX, 2);
                amountX += 44;
            }
            case '8' -> {
                amountX += 5;
                billGraphics2D.drawImage(getAmountImage8(), null, amountX, 0);
                amountX += 48;
            }
            case '9' -> {
                amountX += 5;
                billGraphics2D.drawImage(getAmountImage9(), null, amountX, 2);
                amountX += 48;
            }
        }
    }

    /**
     * 画支付信息
     *
     * @param billGraphics2D billGraphics2D
     * @param paymentMode    转账方式
     * @param transferTime   转账时间
     */
    protected abstract void drawPaymentInfo(Graphics2D billGraphics2D, String paymentMode, String transferTime);

    /**
     * 画上金额
     *
     * @param billGraphics2D billGraphics2D
     * @param amount         金额
     */
    private void drawAmount(Graphics2D billGraphics2D, String amount) {
        amountX = 0;
        //画上转账金额
        BufferedImage amountImage = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D amountGraphics2D = amountImage.createGraphics();

        //填充背景为白色
        amountGraphics2D.fillRect(0, 0, 400, 200);

        //画出-
        drawAmountCharacter(amountGraphics2D, '-');

        //为每一个字符画画
        char[] chars = amount.toCharArray();
        for (char amountChar : chars) {
            drawAmountCharacter(amountGraphics2D, amountChar);
        }

        //获取画幅最小的转账金额图片
        amountImage = Painter.cutWhiteImage(amountImage, Color.white);

        //在指定位置画上金额
        int screenWidth = 1176;
        int amountImageWidth = amountImage.getWidth();
        int amountImageX = (screenWidth - amountImageWidth) / 2;
        billGraphics2D.drawImage(amountImage, null, amountImageX, 606);
    }

    /**
     * 画尾部
     *
     * @param billGraphics2D billGraphics2D
     */
    protected abstract void drawTail(Graphics2D billGraphics2D);
}
